#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, dist[50001];
struct Node {
	ll y, v;
	Node (ll _y, ll _v) {
		y = _y;
		v = _v;
	}
};
vector<Node> edge[50001];
bool b[50001];
set < pair<ll, ll>> q;
inline void Prim() {
	memset(dist, 127, sizeof(dist));
	memset(b, false, sizeof(b));
	dist[1] = 0;
	for (ll i = 1; i <= n; ++i)
		q.insert({dist[i], i});
	ll ans = 0;
	for (ll i = 1; i <= n; ++i) {
		ll x = q.begin()->second;
		q.erase(q.begin());
		ans += dist[x];
		b[x] = true;
		for (auto j : edge[x])
			if (!b[j.y] && j.v < dist[j.y]) {
				q.erase({dist[j.y], j.y});
				dist[j.y] = j.v;
				q.insert({dist[j.y], j.y});
			}	
	}
	printf("%lld\n", ans);
}

int main() {
	scanf("%lld%lld", &n, &m);
	for (ll i = 1;  i <= m; ++i) {
		ll x, y, z;
		scanf("%lld%lld%lld", &x, &y, &z);
		edge[x].push_back(Node(y, z));
		edge[y].push_back(Node(x, z));
	}
	Prim();
	return 0;
}
